package org.wattdepot.cli.commands;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Contains methods that list power at given day.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class ListPowerDay extends UserCommandInterface implements UserCommands {

  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output =
          listPowerDay(inputString[1], inputString[2], inputString[4], inputString[6],
              inputString[8]);
    }
    catch (WattDepotClientException e) {
      System.out.println("List summary error.");
    }
    return output;
  }

  /**
   * Returns the maximum, minimum, or average power generated (or consumed) by the source for the
   * day. The statistic is obtained by sampling the source at the specified minute intervals for the
   * course of the day.
   * 
   * @param power The desired type of power the user wants to view, either "generated" or "consumed"
   * @param sourceName Name of one of the public sources is the system.
   * @param day A date inputed with format YYYY-MM-DD
   * @param minutes The Sample-interval time in minutes
   * @param statistic The desired type of statistic: max, min, or average
   * @throws WattDepotClientException Throws exception.
   * @return String of the results
   */
  public String listPowerDay(String power, String sourceName, String day, String minutes,
      String statistic) throws WattDepotClientException {
    int interval = Integer.parseInt(minutes);
    int minutesInOneDay = 1440;
    int timesToCheck = (minutesInOneDay / interval);
    String newLine = "\n";
    double currentMax = 0;
    double currentMin = 0;
    double nextPowerGenerated = 0;
    double powersGenerated = 0;
    ValidateDay date = new ValidateDay();
    StringBuffer buffer = new StringBuffer();
    if (sourceExists(sourceName)) {
      // validates the timestamp into XMLGregorianCalendar
      XMLGregorianCalendar input = date.validateDay(day);

      if (statistic.equalsIgnoreCase("max") && (power.equalsIgnoreCase(GENERATED))) {
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerGenerated(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // the currentMax gets the new maximum value if nextPowerGenerated is greater
          if (nextPowerGenerated > currentMax) {
            currentMax = nextPowerGenerated;
          }
        }
        buffer.append(currentMax);

      }

      else if (statistic.equalsIgnoreCase("min") && (power.equalsIgnoreCase(GENERATED))) {
        currentMin = (int) getClient().getPowerGenerated(sourceName, input);
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerGenerated(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // the currentMin gets the new maximum value if currentMin is greater
          if (nextPowerGenerated < currentMin) {
            currentMin = nextPowerGenerated;
          }
        }
        // System.out.println(currentMin);
        buffer.append(currentMin);
      }

      else if (statistic.equalsIgnoreCase("average") && (power.equalsIgnoreCase(GENERATED))) {
        double average = 0;
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerGenerated(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // adding all the currentMax value
          powersGenerated = powersGenerated + nextPowerGenerated;
        }
        // gets the average of all the powers generated
        average = powersGenerated / timesToCheck;
        buffer.append(average);
      }
      if (statistic.equalsIgnoreCase("max") && (power.equalsIgnoreCase(CONSUMED))) {
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerConsumed(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // the currentMax gets the new maximum value if nextPowerGenerated is greater
          if (nextPowerGenerated > currentMax) {
            currentMax = nextPowerGenerated;
          }
        }
        buffer.append(currentMax);

      }

      else if (statistic.equalsIgnoreCase("min") && (power.equalsIgnoreCase(CONSUMED))) {
        currentMin = (int) getClient().getPowerGenerated(sourceName, input);
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerConsumed(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // the currentMin gets the new maximum value if currentMin is greater
          if (nextPowerGenerated < currentMin) {
            currentMin = nextPowerGenerated;
          }
        }
        buffer.append(currentMin);
      }

      else if (statistic.equalsIgnoreCase("average") && (power.equalsIgnoreCase(CONSUMED))) {
        double average = 0;
        for (int i = 0; i < timesToCheck; i++) {
          nextPowerGenerated = (int) getClient().getPowerConsumed(sourceName, input);
          input = Tstamp.incrementMinutes(input, interval);
          // adding all the currentMax value
          powersGenerated = powersGenerated + nextPowerGenerated;
        }
        // gets the average of all the powers generated
        average = powersGenerated / timesToCheck;
        buffer.append(average);
      }
    }
    buffer.append(newLine);
    return buffer.toString();
  }
}
